package com.macro.mall.portal.service.impl;

import com.macro.mall.mapper.VersionMapper;
import com.macro.mall.model.Version;
import com.macro.mall.portal.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class VersionServiceImpl implements VersionService {

    @Autowired
    private VersionMapper versionMapper;

    @Override
    public Map<String, Object> update(String name, String version) {
        Version obj = Version.builder().name(name).build();
        Version result = versionMapper.selectVersionObjSingle(obj);
        if (!StringUtils.isEmpty(result)&&!StringUtils.isEmpty(result.getVersion())) {
            String[] currentVersions = version.split("\\.");
            String[] resultVersions = result.getVersion().split("\\.");
            if (version.equals(result.getVersion())) {
                Map<String,Object> map = new HashMap();
                map.put("update", false);
                map.put("wgtUrl", "");
                map.put("pkgUrl", "");
                return map;
            } else if (Integer.parseInt(currentVersions[0])<Integer.parseInt(resultVersions[0])) {
                Map<String,Object> map = new HashMap();
                map.put("update", true);
                map.put("wgtUrl", "");
                map.put("pkgUrl", result.getPkgUrl());
                return map;
            } else {
                Map<String,Object> map = new HashMap();
                map.put("update", true);
                map.put("wgtUrl", result.getWgtUrl());
                map.put("pkgUrl", "");
                return map;
            }
        }
        Map<String,Object> map = new HashMap();
        map.put("update", false);
        map.put("wgtUrl", "");
        map.put("pkgUrl", "");
        return map;
    }
}
